using System;
using System.Collections.Generic;
using System.Drawing;
using Infragistics.UltraChart.Resources.Appearance;
using PluginFrame;

namespace PcrDataStructure
{
    [Serializable]
    public class AlleleExperiment : PcrExperiment, IAlleleExperiment, IPcrExperiment
    {
        [NonSerialized]
        private XYSeries[][] mAlleleDataSeries;

        [NonSerialized]
        private string[][] mAlleleItemTargets;

        [NonSerialized]
        private Dictionary<int, XYDataPoint>[] mAllele1CellPoint;

        [NonSerialized]
        private Dictionary<int, XYDataPoint>[] mAllele2CellPoint;

        [NonSerialized]
        private Dictionary<int, XYDataPoint>[] mAllelePolarCellPoint;

        [NonSerialized]
        private XYSeries[][][] mBackGroundCellSeries;

        protected AllelePointsColorStyle mPointsColorStyle;

        protected bool mIsShowBackGround;

        [NonSerialized]
        protected int mFirstAvailableChannelIndex;

        public int FirstAvailableChannelIndex
        {
            get
            {
                return mFirstAvailableChannelIndex;
            }
            set
            {
                mFirstAvailableChannelIndex = value;
            }
        }

        public bool IsShowBackGround
        {
            get
            {
                return mIsShowBackGround;
            }
            set
            {
                mIsShowBackGround = value;
            }
        }

        public AllelePointsColorStyle PointsColorStyle
        {
            get
            {
                return mPointsColorStyle;
            }
            set
            {
                mPointsColorStyle = value;
            }
        }

        public XYSeries[][] GetBackGroundCellSeries(int index)
        {
            if (mBackGroundCellSeries == null)
            {
                InitializeAlleleDataSeries();
            }
            if (index < mBackGroundCellSeries.Length)
            {
                return mBackGroundCellSeries[index];
            }
            return null;
        }

        public Dictionary<int, XYDataPoint> GetAllele1CellPoint(int index)
        {
            if (mAllele1CellPoint == null)
            {
                InitializeAlleleDataSeries();
            }
            if (index < mAllele1CellPoint.Length)
            {
                return mAllele1CellPoint[index];
            }
            return null;
        }

        public Dictionary<int, XYDataPoint> GetAllele2CellPoint(int index)
        {
            if (mAllele2CellPoint == null)
            {
                InitializeAlleleDataSeries();
            }
            if (index < mAllele2CellPoint.Length)
            {
                return mAllele2CellPoint[index];
            }
            return null;
        }

        public Dictionary<int, XYDataPoint> GetAllelePolarCellPoint(int index)
        {
            if (mAllelePolarCellPoint == null)
            {
                InitializeAlleleDataSeries();
            }
            if (index < mAllelePolarCellPoint.Length)
            {
                return mAllelePolarCellPoint[index];
            }
            return null;
        }

        public string[] GetAlleleItemTargets(int index)
        {
            if (mAlleleItemTargets == null)
            {
                InitializeAlleleDataSeries();
            }
            if (index < mAlleleItemTargets.Length)
            {
                return mAlleleItemTargets[index];
            }
            return null;
        }

        public XYSeries[] GetAlleleDataSeries(int index)
        {
            if (mAlleleDataSeries == null)
            {
                InitializeAlleleDataSeries();
            }
            if (index < mAlleleDataSeries.Length)
            {
                return mAlleleDataSeries[index];
            }
            return null;
        }

        public AlleleExperiment(IPcrStructure pcrStructure)
            : base(pcrStructure)
        {
            mSystemType = PcrSystemType.Allele;
            mIsShowBackGround = true;
        }

        public void SetBackGroundSeriesVisible(bool isVisible, bool isSelectCellVisible)
        {
            foreach (IPcrExperimentProject mProject in mProjects)
            {
                if (mProject == null)
                {
                    continue;
                }
                foreach (IPcrCell cell in mProject.Cells)
                {
                    if (cell == null || cell.IsBad)
                    {
                        continue;
                    }
                    XYSeries[] alleleSeries = cell.AlleleSeries;
                    if (alleleSeries == null)
                    {
                        continue;
                    }
                    for (int i = 0; i < alleleSeries.Length; i++)
                    {
                        if (alleleSeries[i] == null || alleleSeries[i].PEs.Count == 0)
                        {
                            continue;
                        }
                        PaintElement paintElement = alleleSeries[i].PEs[0];
                        Color backGroundColor = mConfigurationService.AllelePointsConfiguration.BackGroundColor;
                        if (isSelectCellVisible)
                        {
                            if (paintElement.Fill.Equals(backGroundColor))
                            {
                                alleleSeries[i].Visible = isVisible;
                            }
                        }
                        else
                        {
                            alleleSeries[i].Visible = isVisible;
                            alleleSeries[i].PEs.Clear();
                            alleleSeries[i].PEs.Add(new PaintElement(backGroundColor));
                        }
                    }
                }
            }
        }

        public override void CheckNewColumnExist()
        {
        }

        protected override bool CheckColumnInExperiment(IColumn column)
        {
            if (column == null)
            {
                return false;
            }
            if (column.Name == "Allele1Key" || column.Name == "Allele2Key")
            {
                return false;
            }
            if (column.Classification != ColumnClassification.RelativeQuantificationResult && column.Classification != ColumnClassification.GeneTypeResult && column.Classification != ColumnClassification.GeneScanResult && column.Classification != ColumnClassification.AnalysisResult && column.Classification != ColumnClassification.CalculationResult)
            {
                return column.Classification != ColumnClassification.MeltingCurveResult;
            }
            return false;
        }

        protected override List<IPcrExperimentProject> CheckExperimentImportStandardCurve()
        {
            return null;
        }

        protected override bool GetProjectMeltingStatus(IPcrExperimentProject project, out IMeltingSegment meltingSegment)
        {
            meltingSegment = null;
            return false;
        }

        protected override void InitializeProjectItemOption(ref IAbsoluteAnalysisParameterConfiguration analysisOption, ref IMeltingAnalysisParameterConfiguration meltingOption, IProjectItem projectItem, IPcrExperimentProject project, int tubeIndex)
        {
            analysisOption = projectItem.TubeAnalysisParameterConfiguration[0] as IAbsoluteAnalysisParameterConfiguration;
            analysisOption.Project = project;
            (analysisOption as AnalysisParameterConfiguration).IsNeedReAnalysis = true;
            (analysisOption as AnalysisParameterConfiguration).ProjectChannel = projectItem.ChannelConfiguration.Position;
            meltingOption = null;
        }

        private void InitializeAlleleDataSeries()
        {
            mAlleleDataSeries = new XYSeries[mProjects.Count][];
            mAlleleItemTargets = new string[mProjects.Count][];
            mAllele1CellPoint = new Dictionary<int, XYDataPoint>[mProjects.Count];
            mAllele2CellPoint = new Dictionary<int, XYDataPoint>[mProjects.Count];
            mAllelePolarCellPoint = new Dictionary<int, XYDataPoint>[mProjects.Count];
            mBackGroundCellSeries = new XYSeries[mProjects.Count][][];
            for (int i = 0; i < mAlleleDataSeries.Length; i++)
            {
                mAlleleItemTargets[i] = new string[2];
                mAllele1CellPoint[i] = new Dictionary<int, XYDataPoint>();
                mAllele2CellPoint[i] = new Dictionary<int, XYDataPoint>();
                mAllelePolarCellPoint[i] = new Dictionary<int, XYDataPoint>();
                mAlleleDataSeries[i] = new XYSeries[Enum.GetNames(typeof(AlleleCallResult)).GetLength(0)];
                mBackGroundCellSeries[i] = new XYSeries[mAlleleDataSeries[i].Length][];
                for (int j = 0; j < mAlleleDataSeries[i].Length; j++)
                {
                    mAlleleDataSeries[i][j] = new XYSeries();
                    mAlleleDataSeries[i][j].PEs.Add(new PaintElement(mConfigurationService.AllelePointsConfiguration.AlleleResultColor[j]));
                    mBackGroundCellSeries[i][j] = new XYSeries[3];
                    for (int k = 0; k < 3; k++)
                    {
                        mBackGroundCellSeries[i][j][k] = new XYSeries();
                        mBackGroundCellSeries[i][j][k].Key = "SelectedSeries";
                        mBackGroundCellSeries[i][j][k].PEs.Add(new PaintElement(mConfigurationService.AllelePointsConfiguration.BackGroundColor));
                    }
                }
            }
        }

        protected override void CalculateSpecialData(string analyzeMethod, IPcrExperimentProject project, int projectIndex)
        {
            if (mAlleleDataSeries == null || mAlleleDataSeries.Length == 0 || mAlleleDataSeries.Length != mProjects.Count)
            {
                InitializeAlleleDataSeries();
            }
            if (analyzeMethod == "Analyze")
            {
                mAnalysisService.AnalyzeAlleleProject(project, projectIndex, this);
            }
        }

        protected override void InitializeMeltingVariables()
        {
        }

        protected override void ClearResultInformation(IPcrCell pcrCell, int channelIndex)
        {
            pcrCell.SetCellInformation("Rn1Key", channelIndex, string.Empty);
            pcrCell.SetCellInformation("Rn2Key", channelIndex, string.Empty);
            pcrCell.SetCellInformation("MethodKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("QualityKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("CallKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("InterpretationKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("MeasurementResultKey", channelIndex, string.Empty);
        }

        protected override bool CheckStandardCell(IPcrCell pcrCell, int channelIndex)
        {
            return true;
        }
    }
}
